{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c50bc614-b608-415b-a0ae-03a7189b02fe",
   "metadata": {},
   "source": [
    "#### PivotTableJS 🚀 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "78fb524c-b199-4107-90e8-1a85cfe92b58",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pivottablejs import pivot_ui"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6b732b88-7eef-4a9d-bea8-6cf9953e38a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Party</th>\n",
       "      <th>Province</th>\n",
       "      <th>Age</th>\n",
       "      <th>Gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liu, Laurin</td>\n",
       "      <td>NDP</td>\n",
       "      <td>Quebec</td>\n",
       "      <td>22.0</td>\n",
       "      <td>Female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Mourani, Maria</td>\n",
       "      <td>Bloc Quebecois</td>\n",
       "      <td>Quebec</td>\n",
       "      <td>43.0</td>\n",
       "      <td>Female</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Name           Party Province   Age  Gender\n",
       "0     Liu, Laurin             NDP   Quebec  22.0  Female\n",
       "1  Mourani, Maria  Bloc Quebecois   Quebec  43.0  Female"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"mps.csv\")\n",
    "df.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fe23d5a9-f422-4b36-b2e0-e539b8f65791",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"500\"\n",
       "            src=\"pivottablejs.html\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x10d2daaa0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivot_ui(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ae919931-6888-4a3b-b0a8-f863a1ae62a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"500\"\n",
       "            src=\"pivottablejs.html\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x117007be0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "\n",
    "pivot_ui(df,      \n",
    "    cols= [\"Party\"],\n",
    "    rows= [\"Province\"],\n",
    "    rendererName= \"Horizontal Stacked Bar Chart\",\n",
    "    rowOrder= \"value_z_to_a\", \n",
    "    colOrder= \"value_z_to_a\",\n",
    "    rendererOptions= {\n",
    "        \"c3\": { \"data\": {\"colors\": {\n",
    "            \"Liberal\": '#dc3912', \"Conservative\": '#3366cc', \"NDP\": '#ff9900',\n",
    "            \"Green\":'#109618', 'Bloc Quebecois': '#990099'\n",
    "        }}}\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6129fd7e-d89d-4586-b346-bf6b8a9a6456",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
